<?php
namespace app\home\controller;

use app\common\model\ConfigModel;
use app\common\model\UserModel;
use think\helper\Str;
use think\Request;

class BaseController extends \think\Controller
{

    protected $siteId;
    protected $webConfig;
    protected $userInfo;

    /**
     * 初始化
     */
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->siteId    = $this->getSiteId();
        $this->webConfig = cache('webconfig' . $this->siteId);
        if(!empty($this->webConfig['HOT_KEYWORDS'])){
            $this->webConfig['HOT_KEYWORDS']=explode(',', $this->webConfig['HOT_KEYWORDS']);
        }
        $this->userInfo  = [];
        if ($this->getMemberUid() > 0) {
            $userModel      = new UserModel();
            $this->userInfo = $userModel->getInfo('member', $this->getMemberUid());
        }
        $this->assign('keyword', $this->webConfig['KEYWORDS']);
        $this->assign('description', $this->webConfig['DESCRIPTION']);
        $this->assign('userInfo', $this->userInfo);
        $this->assign('siteId', $this->siteId);
        $this->assign('webConfig', $this->webConfig);
    }

    /**
     * 获取当前站点site_id
     */
    public function getSiteId()
    {
        if ($this->request->has('site_id')) { //显式传入为主
            return $this->request->param('site_id');
        } else {
            $domain      = $this->request->domain();
            $configModel = new ConfigModel();
            $siteId      = $configModel->where('name', 'DOMAIN')->where('value', 'like', '%' . str_replace('http://', '', $domain) . '%')->value('site_id');
            if (empty($siteId)) {
                return 0;
            }
        }
        return $siteId;
    }

    /*
     * 获取会员Uid
     */
    public function getMemberUid()
    {
        return is_login('member');
    }

    /**
     * 加载模板输出到根目录
     * @access protected
     * @param string $template 模板文件名
     * @param array $vars 模板输出变量
     * @param array $replace 模板替换
     * @param array $config 模板参数
     * @return mixed
     */
    protected function fetch($template = '', $vars = [], $replace = [], $config = [])
    {
        $basePath = ROOT_PATH . 'tpl/' . $this->webConfig['THEME'] . '/';
        $controller = Str::snake($this->request->controller());
        $action = strtolower($this->request->action());
        if (is_mobile() != 2) {
            $templatePath = $basePath . 'mobile/' . $controller . '/';
            if ($template == '') {
                $file = $templatePath . $action . '.html';
            } else {
                $file = $templatePath . $template . '.html';
            }
            if (!file_exists($file)) {
                $templatePath = $basePath . 'pc/' . $controller . '/';
            }
        } else {
            $templatePath = $basePath . 'pc/' . $controller . '/';
        }
        if ($template == '') {
            $template = $templatePath . $action . '.html';
        } else {
            $template = $templatePath . $template . '.html';
        }
        if (empty($replace)) {
            $replace = [
                '__PUBLIC__' => '/public/',
                '__SITE_PUBLIC__' => '/tpl/site_0/' . $this->webConfig['THEME'] . '/public/',
            ];
        }
        return $this->view->fetch($template, $vars, $replace, $config);
    }
}
